package com.grid.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.grid.db.ConnectMysql;
import com.grid.model.Operation;


@Path("/operation")
public class OperationResource {

private ArrayList<Operation> OperationList = new ArrayList<Operation>();
	
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	
	public ArrayList<Operation> getOperationList() {
		return OperationList;
	}

	public void setOperationList(ArrayList<Operation> operationList) {
		OperationList = operationList;
	}

Map<String,Integer> OperationNameMapping = new LinkedHashMap<String, Integer>();
	
	public Map<String, Integer> getOperationNameMapping() {
		return OperationNameMapping;
	}



	public void setOperationNameMapping(Map<String, Integer> operationNameMapping) {
		OperationNameMapping = operationNameMapping;
	}

	private ArrayList<String> OperationNames = new ArrayList<String>();
	private ArrayList<Integer> Operationvalues = new ArrayList<Integer>();
	
	
	public ArrayList<Integer> getOperationvalues() {
		return Operationvalues;
	}



	public void setOperationvalues(ArrayList<Integer> operationvalues) {
		Operationvalues = operationvalues;
	}



	public ArrayList<String> getOperationNames() {
		return OperationNames;
	}



	public void setOperationNames(ArrayList<String> operationNames) {
		OperationNames = operationNames;
	}


	private ArrayList<String> selectedCheckBoxList = new ArrayList<String>();
	
	public ArrayList<String> getSelectedCheckBoxList() {
		return selectedCheckBoxList;
	}

	public void setSelectedCheckBoxList(ArrayList<String> selectedCheckBoxList) {
		this.selectedCheckBoxList = selectedCheckBoxList;
	}


	private ArrayList<String> Overlays = new ArrayList<String>();
	
	public ArrayList<String> getOverlays() {
		return Overlays;
	}

	public void setOverlays(ArrayList<String> overlays) {
		Overlays = overlays;
	}
	
	private String overlay;
	


	public String getOverlay() {
		return overlay;
	}

	public void setOverlay(String overlay) {
		this.overlay = overlay;
	}
	
	
	  @Path("/application/{selectedpplication}")
	  @GET
	  @Consumes(MediaType.TEXT_PLAIN)
	  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	  public ArrayList<Operation> getOperationData(@PathParam ("selectedpplication") String ApplicationName) {
	       
		  System.out.println("yes i was calld with value" + ApplicationName);
		 // Application appl = applicationXml.getValue();
		  
	      //String ApplicationName = appl.getApplicationName();
	      java.sql.ResultSet rs;	
		  String query1 =  "select a.operationname,b.mappingvalue from operationmaster a, application_operation_mapping b,applicationmaster c where a.idoperationmaster = b.operationid and b.applicationID=c.applicationid and c.applicationname = '"+ ApplicationName +"' and b.mappingdescription = '1';";
	      
	      
		  System.out.println(query1);
			
			rs = ConnectMysql.executeQuery(query1);
			try
			{
				while(rs.next())
				{
					
					Operation tempOpp = new Operation();
					
					tempOpp.setOperationName(rs.getString(1));
					tempOpp.setOperationValue(rs.getInt(2));
					
					//OperationNames.add(rs.getString(1));
					
					System.out.println(rs.getString(1));
					System.out.println(rs.getInt(2));
					
					OperationList.add(tempOpp);
					
					//OperationNameMapping.put(rs.getString(1), rs.getInt(2));
				}
				
				
			}
			catch(SQLException ex)
			{
				System.out.println("SQL exception occured");
			}
	       
	      return OperationList;
	                       
	  }
	  
	  
	  @Path("/applicationone/{selectedpplication}")
	  @GET
	  @Consumes(MediaType.TEXT_PLAIN)
	  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	  public ArrayList<Operation> getOperationDataone(@PathParam ("selectedpplication") String ApplicationName) {
	       
		  System.out.println("yes i was calld with value" + ApplicationName);
		 // Application appl = applicationXml.getValue();
		  
	      //String ApplicationName = appl.getApplicationName();
	      java.sql.ResultSet rs;	
		  String query1 =  "select a.operationname,b.mappingvalue from operationmaster a, application_operation_mapping b,applicationmaster c where a.idoperationmaster = b.operationid and b.applicationid=c.applicationid and c.applicationname = '"+ ApplicationName +"';";
	      
	      
		  System.out.println(query1);
			
			rs = ConnectMysql.executeQuery(query1);
			try
			{
				while(rs.next())
				{
					
					Operation tempOpp = new Operation();
					
					tempOpp.setOperationName(rs.getString(1));
					tempOpp.setOperationValue(rs.getInt(2));
					
					//OperationNames.add(rs.getString(1));
					
					//System.out.println(rs.getString(1));
					//System.out.println(rs.getInt(2));
					
					OperationList.add(tempOpp);
					
					//OperationNameMapping.put(rs.getString(1), rs.getInt(2));
				}
				
				
			}
			catch(SQLException ex)
			{
				System.out.println("SQL exception occured");
			}
	       
	      return OperationList;
	                       
	  }

	  
	  @Path("/applicationtwo/{selectedpplication}/{selectedoverlay}")
	  @GET
	  @Consumes(MediaType.TEXT_PLAIN)
	  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	  public ArrayList<Operation> getOperationDatatwo(@PathParam ("selectedpplication") String ApplicationName,@PathParam ("selectedoverlay") String SelectedOverlay) {
	       
		  System.out.println("yes two was calld with value" + ApplicationName);
		 // Application appl = applicationXml.getValue();
		  
	      //String ApplicationName = appl.getApplicationName();
	      java.sql.ResultSet rs;	
		  String query1 =  "select a.operationname,b.mappingvalue from operationmaster a, application_operation_mapping b,applicationmaster c where a.idoperationmaster = b.operationid and b.applicationid=c.applicationid and c.applicationname = '"+ ApplicationName +"' and b.mappingdescription = '"+SelectedOverlay +"';";
	      
	      
		  System.out.println(query1);
			
			rs = ConnectMysql.executeQuery(query1);
			try
			{
				while(rs.next())
				{
					
					Operation tempOpp = new Operation();
					
					tempOpp.setOperationName(rs.getString(1));
					tempOpp.setOperationValue(rs.getInt(2));
					
					//OperationNames.add(rs.getString(1));
					
					System.out.println(rs.getString(1));
					System.out.println(rs.getInt(2));
					
					OperationList.add(tempOpp);
					
					//OperationNameMapping.put(rs.getString(1), rs.getInt(2));
				}
				
				
			}
			catch(SQLException ex)
			{
				System.out.println("SQL exception occured");
			}
	       
	      return OperationList;
	                       
	  }



}
